package com.majun.service.impl;

import com.majun.entity.RoleBO;
import com.majun.entity.RoleResourceRelation;
import com.majun.mapper.RoleResourceRelationMapper;
import com.majun.service.RoleResourceRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleResourceRelationImpl implements RoleResourceRelationService {

    @Autowired
    private RoleResourceRelationMapper roleResourceRelationMapper;

    @Override
    public List<Integer> selectByRoleID(Integer id) {
        return roleResourceRelationMapper.selectByRoleID(id);
    }

    @Override
    public void deleteByRoleID(Integer id) {
        roleResourceRelationMapper.deleteByRoleID(id);
    }

    @Override
    public int updateResourceByRoleID(RoleBO roleBO) {
        roleResourceRelationMapper.deleteByRoleID(roleBO.getRoleId());

        for (Integer resourcesID : roleBO.getResourcesID()){
            RoleResourceRelation roleResource = new RoleResourceRelation();
            roleResource.setRoleid(roleBO.getRoleId());
            roleResource.setResid(resourcesID);

            roleResourceRelationMapper.insert(roleResource);

        }

        return 0;
    }
}
